﻿using System;
using System.IO;
using SEXY;
using UniRx;
using UniRx.Triggers;
using UnityEngine;

namespace Studio
{
	// Token: 0x0200042E RID: 1070
	public class BackgroundCtrl : MonoBehaviour
	{
		// Token: 0x17000280 RID: 640
		// (get) Token: 0x06001A5F RID: 6751 RVA: 0x000A8700 File Offset: 0x000A6900
		// (set) Token: 0x06001A60 RID: 6752 RVA: 0x000A8708 File Offset: 0x000A6908
		public bool isVisible
		{
			get
			{
				return this.m_IsVisible;
			}
			set
			{
				this.m_IsVisible = value;
				this.meshRenderer.enabled = value;
			}
		}

		// Token: 0x17000281 RID: 641
		// (get) Token: 0x06001A61 RID: 6753 RVA: 0x000A8720 File Offset: 0x000A6920
		private Camera mainCamera
		{
			get
			{
				if (this.m_Camera == null)
				{
					this.m_Camera = Camera.main;
				}
				return this.m_Camera;
			}
		}

		// Token: 0x06001A62 RID: 6754 RVA: 0x000A8744 File Offset: 0x000A6944
		public bool Load(string _file)
		{
			string path = UserData.Path + "background/" + _file;
			if (!File.Exists(path))
			{
				this.isVisible = false;
				Singleton<Studio>.Instance.sceneInfo.background = string.Empty;
				return false;
			}
			Texture texture = PngAssist.LoadTexture(path);
			if (texture == null)
			{
				this.isVisible = false;
				return false;
			}
			Material material = this.meshRenderer.material;
			material.SetTexture("_MainTex", texture);
			this.meshRenderer.material = material;
			this.isVisible = true;
			Singleton<Studio>.Instance.sceneInfo.background = _file;
			Resources.UnloadUnusedAssets();
			GC.Collect();
			return true;
		}

		// Token: 0x06001A63 RID: 6755 RVA: 0x000A87F0 File Offset: 0x000A69F0
		private void Reflect()
		{
			Vector3[] vertices = this.meshFilter.mesh.vertices;
			float num = this.mainCamera.fieldOfView / 2f;
			float angle = Mathf.Atan(Mathf.Tan(0.017453292f * num) * this.mainCamera.aspect) * 57.29578f;
			Plane plane = new Plane(Vector3.back, this.mainCamera.farClipPlane - 2f);
			Vector3 vector = this.Raycast(plane, Vector3.forward);
			Vector3 vector2 = this.Raycast(plane, Quaternion.AngleAxis(angle, Vector3.up) * Vector3.forward);
			Vector3 vector3 = this.Raycast(plane, Quaternion.AngleAxis(num, Vector3.right) * Vector3.forward);
			vertices[0] = new Vector3(vector2.x, -vector3.y, vector.z);
			vertices[1] = new Vector3(-vector2.x, vector3.y, vector.z);
			vertices[2] = new Vector3(-vector2.x, -vector3.y, vector.z);
			vertices[3] = new Vector3(vector2.x, vector3.y, vector.z);
			this.meshFilter.mesh.vertices = vertices;
			this.meshFilter.mesh.RecalculateBounds();
			this.oldFOV = this.mainCamera.fieldOfView;
		}

		// Token: 0x06001A64 RID: 6756 RVA: 0x000A897C File Offset: 0x000A6B7C
		private Vector3 Raycast(Plane _plane, Vector3 _dir)
		{
			float d = 0f;
			_plane.Raycast(new Ray(Vector3.zero, _dir), out d);
			return _dir * d;
		}

		// Token: 0x06001A65 RID: 6757 RVA: 0x000A89AC File Offset: 0x000A6BAC
		private void Start()
		{
			this.isVisible = false;
			(from _ in this.LateUpdateAsObservable()
			where this.isVisible && this.oldFOV != this.mainCamera.fieldOfView
			select _).Subscribe(delegate(Unit _)
			{
				this.Reflect();
			}).AddTo(this);
		}

		// Token: 0x04001D32 RID: 7474
		[SerializeField]
		private MeshFilter meshFilter;

		// Token: 0x04001D33 RID: 7475
		[SerializeField]
		private MeshRenderer meshRenderer;

		// Token: 0x04001D34 RID: 7476
		private bool m_IsVisible;

		// Token: 0x04001D35 RID: 7477
		private Camera m_Camera;

		// Token: 0x04001D36 RID: 7478
		private float oldFOV;
	}
}
